package cn.stylefeng.guns.modular.manager.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.manager.entity.*;
import cn.stylefeng.guns.modular.manager.model.params.FlowMemberCouponActivityParam;
import cn.stylefeng.guns.modular.manager.service.*;
import cn.stylefeng.guns.utils.CommonUtil;
import cn.stylefeng.guns.utils.DateUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


/**
 * 会员红包活动控制器
 *
 * @author yumker
 * @Date 2021-01-07 17:22:24
 */
@Controller
@RequestMapping("/flowMemberCouponActivity")
public class FlowMemberCouponActivityController extends BaseController {

    private String PREFIX = "/manager/flowMemberCouponActivity";

    @Autowired
    private FlowMemberCouponActivityService flowMemberCouponActivityService;
    @Autowired
    private FlowServiceService flowServiceService;

    @Autowired
    FlowMemberCouponActivityRecordService flowMemberCouponActivityRecordService;
    @Autowired
    FlowCouponService flowCouponService;
    @Autowired
    FlowBdMemberService flowBdMemberService;
    @Autowired
    FlowZfbMemberService flowZfbMemberService;
    /**
     * 跳转到主页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/flowMemberCouponActivity.html";
    }

    /**
     * 新增页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/flowMemberCouponActivity_add.html";
    }

    /**
     * 编辑页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/flowMemberCouponActivity_edit.html";
    }

    /**
     * 新增接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FlowMemberCouponActivityParam flowMemberCouponActivityParam) {
        this.flowMemberCouponActivityService.add(flowMemberCouponActivityParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FlowMemberCouponActivityParam flowMemberCouponActivityParam) {
        this.flowMemberCouponActivityService.update(flowMemberCouponActivityParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FlowMemberCouponActivityParam flowMemberCouponActivityParam) {
        this.flowMemberCouponActivityService.delete(flowMemberCouponActivityParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FlowMemberCouponActivityParam flowMemberCouponActivityParam) {
        FlowMemberCouponActivity detail = this.flowMemberCouponActivityService.getById(flowMemberCouponActivityParam.getCouponActivityId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FlowMemberCouponActivityParam flowMemberCouponActivityParam) {
        return this.flowMemberCouponActivityService.findPageBySpec(flowMemberCouponActivityParam);
    }

    @ResponseBody
    @RequestMapping("/serviceType")
    public ResponseData serviceType(){
        return ResponseData.success(flowServiceService.list());
    }

    @ResponseBody
    @RequestMapping("/toUser")
    public ResponseData toUser(FlowMemberCouponActivityParam flowMemberCouponActivityParam){
        FlowMemberCouponActivity detail = this.flowMemberCouponActivityService.getById(flowMemberCouponActivityParam.getCouponActivityId());
        if(detail == null){
            return ResponseData.error("不存在该用户");
        }
        //查询所有的用户
        QueryWrapper<FlowZfbMember> flowZfbMemberQueryWrapper = new QueryWrapper<>();
        List<FlowZfbMember> flowZfbMembers = flowZfbMemberService.list(flowZfbMemberQueryWrapper);
        //循环每个用户，去查询用户是否已领取卷
        for (int i = 0;i<flowZfbMembers.size();i++){
            //查询
            QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
            flowMemberCouponActivityRecordQueryWrapper.eq("coupon_activity_id",detail.getCouponActivityId());
            flowMemberCouponActivityRecordQueryWrapper.eq("type",0);
            flowMemberCouponActivityRecordQueryWrapper.eq("member_id",flowZfbMembers.get(i).getMemberId());
            int count = flowMemberCouponActivityRecordService.count(flowMemberCouponActivityRecordQueryWrapper);
            if(count == 0){
                //领取
                FlowCoupon flowCoupon = new FlowCoupon();
                flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                flowCoupon.setCouponTitle(detail.getActivityTitle());
                flowCoupon.setCreateTime(new Date());
                flowCoupon.setIsGet(1);
                flowCoupon.setType(0);
                flowCoupon.setMemberId(flowZfbMembers.get(i).getMemberId());
                flowCoupon.setUpdateTime(new Date());
                flowCoupon.setGetTime(new Date());
                flowCoupon.setIsUsed(0);
                flowCoupon.setUseTime(new Date());
                flowCoupon.setOutDay(detail.getOutDate());
                flowCoupon.setMoneys(detail.getPrice());
                flowCoupon.setOnprice(detail.getOnprice());
                if(detail.getOutDate().intValue() > 0){
                    flowCoupon.setEndTime(DateUtil.addDay(new Date(),detail.getOutDate().intValue()));
                }else {
                    flowCoupon.setEndTime(new Date());
                }
                flowCoupon.setServiceType(detail.getServiceType());
                flowCouponService.save(flowCoupon);
                //领取记录
                FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                flowMemberCouponActivityRecord.setCouponActivityId(detail.getCouponActivityId());
                flowMemberCouponActivityRecord.setCreateTime(new Date());
                flowMemberCouponActivityRecord.setMemberId(flowZfbMembers.get(i).getMemberId());
                flowMemberCouponActivityRecord.setUpdateTime(new Date());
                flowMemberCouponActivityRecord.setType(0);
                flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
            }
        }
        //查询所有的用户
        QueryWrapper<FlowBdMember> flowBdMemberQueryWrapper = new QueryWrapper<>();
        List<FlowBdMember> flowBdMembers = flowBdMemberService.list(flowBdMemberQueryWrapper);
        //循环每个用户，去查询用户是否已领取卷
        for (int i = 0;i<flowBdMembers.size();i++){
            //查询
            QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
            flowMemberCouponActivityRecordQueryWrapper.eq("coupon_activity_id",detail.getCouponActivityId());
            flowMemberCouponActivityRecordQueryWrapper.eq("member_id",flowBdMembers.get(i).getMemberId());
            flowMemberCouponActivityRecordQueryWrapper.eq("type",1);
            int count = flowMemberCouponActivityRecordService.count(flowMemberCouponActivityRecordQueryWrapper);
            if(count == 0){
                //领取
                FlowCoupon flowCoupon = new FlowCoupon();
                flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                flowCoupon.setCouponTitle(detail.getActivityTitle());
                flowCoupon.setCreateTime(new Date());
                flowCoupon.setIsGet(1);
                flowCoupon.setType(1);
                flowCoupon.setMemberId(flowBdMembers.get(i).getMemberId());
                flowCoupon.setUpdateTime(new Date());
                flowCoupon.setGetTime(new Date());
                flowCoupon.setIsUsed(0);
                flowCoupon.setUseTime(new Date());
                flowCoupon.setOutDay(detail.getOutDate());
                flowCoupon.setMoneys(detail.getPrice());
                flowCoupon.setOnprice(detail.getOnprice());
                if(detail.getOutDate().intValue() > 0){
                    flowCoupon.setEndTime(DateUtil.addDay(new Date(),detail.getOutDate().intValue()));
                }else {
                    flowCoupon.setEndTime(new Date());
                }
                flowCoupon.setServiceType(detail.getServiceType());
                flowCouponService.save(flowCoupon);
                //领取记录
                FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                flowMemberCouponActivityRecord.setCouponActivityId(detail.getCouponActivityId());
                flowMemberCouponActivityRecord.setCreateTime(new Date());
                flowMemberCouponActivityRecord.setType(1);
                flowMemberCouponActivityRecord.setMemberId(flowBdMembers.get(i).getMemberId());
                flowMemberCouponActivityRecord.setUpdateTime(new Date());
                flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
            }
        }

        return ResponseData.success("操作成功");
    }
}


